<?php
/*
 * @author HX<titanrain@163.com>
 */
class Page{
	private $pageno;
	private $pagesize;
	private $offset;
	private $pagecount;
	private $totalrecord;
	private $recordlist;
	private $pagebar;
	private $pagebarstart;
	private $pagebarend;
	private $pagebarindexes;
	private $prevpage;
	private $nextpage;
	
	public function __construct($pageno,$pagesize){
		$this->pageno = $pageno;
		$this->pagesize = $pagesize;
		$this->offset = ($pageno-1)*$pagesize;
		if($this->offset<0){
			$this->offset = 0;
		}
	}

	public function LimitStr(){
		return ' limit '.$this->offset.','.$this->pagesize;
	}
	
	private function buildPagebarIndexes(){
		$arr = array();
		for($i=$this->pagebarstart;$i<=$this->pagebarend;$i++){
			$arr[] = $i;
		}
		$this->pagebarindexes = $arr;
	}
	
	public function __get($args){
		return $this->$args;
	}
	
	public function __set($key,$value){
		$this->$key = $value;
		if($key == 'totalrecord'){
			$this->pagecount = intval(($this->totalrecord + $this->pagesize - 1 )/$this->pagesize);
			
			$indexcnt = Yii::app()->params['pagebarLength'];
            $prevpage = $this->pageno - 1;
            $prevpage = $prevpage <=0 ? 1 : $prevpage;
            $this->prevpage = $prevpage;
            $nextpage = $this->pageno + 1;
            $nextpage = $nextpage > $this->pagecount ? $this->pagecount : $nextpage;
            $this->nextpage=$nextpage;

            $pagebarStr = '<a href="javascript:void(0)" pageno="'.$prevpage.'" class="next">上一页</a>';
            if($this->pagecount <= $indexcnt){
                for($i=1;$i<=$this->pagecount;$i++){
                    if($i==$this->pageno){
                        $pagebarStr .= '<span>'.$i.'</span>';
                    }else{
                        $pagebarStr .= '<a href="javascript:void(0)" pageno="'.$i.'">'.$i.'</a>';
                    }
                }
                $this->pagebarstart=1;
                $this->pagebarend =$this->pagecount; 
                self::buildPagebarIndexes();
            }else{
                $oushu = $indexcnt % 2 == 0;
                if($oushu){
                    $left = intval($indexcnt / 2) - 1;
                    $right = $left + 1; 
                    $start =$this->pageno-$left; 
                    while($start<=0){
                        $start++;
                        $right++;
                    }
                    $rightTop = $this->pageno + $right;
                    while($rightTop > $this->pagecount){
                        $rightTop--;
                        $start--;
                    }
                    for($i=$start;$i<$this->pageno;$i++){
                        $pagebarStr .= '<a href="javascript:void(0)" pageno="'.$i.'">'.$i.'</a>';
                    }
                    $pagebarStr .= '<span>'.$this->pageno.'</span>';
                    for($i=$this->pageno+1;$i<=$rightTop && $i<=$this->pagecount;$i++){
                        $pagebarStr .= '<a href="javascript:void(0)" pageno="'.$i.'">'.$i.'</a>';
                    }
                }else{
                    $left = intval($indexcnt / 2);
                    $right = $left;
                    $start =$this->pageno - $left; 
                    while($start<=0){
                        $start++;
                        $right++;
                    }
                    //$
                    $rightTop = $this->pageno + $right;
                    while($rightTop > $this->pagecount){
                        $rightTop--;
                        $start--;
                    }
                    for($i=$start;$i<$this->pageno;$i++){
                        $pagebarStr .= '<a href="javascript:void(0)" pageno="'.$i.'">'.$i.'</a>';
                    }
                    $pagebarStr .= '<span>'.$this->pageno.'</span>';
                    for($i=$this->pageno+1;$i<=$rightTop && $i <= $this->pagecount;$i++){
                        $pagebarStr .= '<a href="javascript:void(0)" pageno="'.$i.'">'.$i.'</a>';
                    }
                }
                $this->pagebarstart=$start;
            	$this->pagebarend =$rightTop;
            	self::buildPagebarIndexes();
            }
            $pagebarStr .= '<a href="javascript:void(0)" pageno="'.$nextpage.'" class="next">下一页</a>';
            $this->pagebar = $pagebarStr;
		}
	}
}
